﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{

    internal class Program
    {
        static OrderService orderService = new OrderService();

        static void Main(string[] args)
        {
            // 初始化一些测试数据
            InitializeTestData();

            while (true)
            {
                Console.WriteLine("\n订单管理系统");
                Console.WriteLine("1. 添加订单");
                Console.WriteLine("2. 删除订单");
                Console.WriteLine("3. 修改订单");
                Console.WriteLine("4. 查询订单");
                Console.WriteLine("5. 显示所有订单");
                Console.WriteLine("6. 排序订单");
                Console.WriteLine("7. 导出订单");
                Console.WriteLine("8. 导入订单");
                Console.WriteLine("0. 退出");
                Console.Write("请选择操作: ");

                string choice = Console.ReadLine();

                try
                {
                    switch (choice)
                    {
                        case "1":
                            AddOrder();
                            break;
                        case "2":
                            RemoveOrder();
                            break;
                        case "3":
                            UpdateOrder();
                            break;
                        case "4":
                            QueryOrders();
                            break;
                        case "5":
                            DisplayAllOrders();
                            break;
                        case "6":
                            SortOrders();
                            break;
                        case "0":
                            return;
                        default:
                            Console.WriteLine("无效的选择，请重新输入。");
                            break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"发生错误: {ex.Message}");
                }
            }
        }

        static void InitializeTestData()
        {
            // 创建一些产品和客户
            Product p1 = new Product("P001", "笔记本电脑", 5999);
            Product p2 = new Product("P002", "手机", 3999);
            Product p3 = new Product("P003", "耳机", 299);

            Customer c1 = new Customer("C001", "张三");
            Customer c2 = new Customer("C002", "李四");

            // 创建订单并添加明细
            Order o1 = new Order("O001", c1, DateTime.Now);
            o1.AddDetail(new OrderDetails(p1, 1));
            o1.AddDetail(new OrderDetails(p3, 2));

            Order o2 = new Order("O002", c2, DateTime.Now.AddDays(-1));
            o2.AddDetail(new OrderDetails(p2, 1));
            o2.AddDetail(new OrderDetails(p3, 1));

            // 添加到订单服务
            orderService.AddOrder(o1);
            orderService.AddOrder(o2);
        }

        static void AddOrder()
        {
            Console.WriteLine("\n添加新订单");

            Console.Write("输入订单号: ");
            string orderId = Console.ReadLine();

            Console.Write("输入客户ID: ");
            string customerId = Console.ReadLine();

            Console.Write("输入客户姓名: ");
            string customerName = Console.ReadLine();

            Customer customer = new Customer(customerId, customerName);
            Order order = new Order(orderId, customer, DateTime.Now);

            while (true)
            {
                Console.Write("是否添加商品明细? (y/n): ");
                if (Console.ReadLine().ToLower() != "y") break;

                Console.Write("输入商品ID: ");
                string productId = Console.ReadLine();

                Console.Write("输入商品名称: ");
                string productName = Console.ReadLine();

                Console.Write("输入商品价格: ");
                decimal price = decimal.Parse(Console.ReadLine());

                Console.Write("输入数量: ");
                int quantity = int.Parse(Console.ReadLine());

                Product product = new Product(productId, productName, price);
                OrderDetails detail = new OrderDetails(product, quantity);

                try
                {
                    order.AddDetail(detail);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine($"错误: {ex.Message}");
                }
            }

            try
            {
                orderService.AddOrder(order);
                Console.WriteLine("订单添加成功!");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"错误: {ex.Message}");
            }
        }

        static void RemoveOrder()
        {
            Console.WriteLine("\n删除订单");
            Console.Write("输入要删除的订单号: ");
            string orderId = Console.ReadLine();

            try
            {
                orderService.RemoveOrder(orderId);
                Console.WriteLine("订单删除成功!");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"错误: {ex.Message}");
            }
        }

        static void UpdateOrder()
        {
            Console.WriteLine("\n修改订单");
            Console.Write("输入要修改的订单号: ");
            string orderId = Console.ReadLine();

            var orders = orderService.QueryByOrderId(orderId);
            if (orders.Count == 0)
            {
                Console.WriteLine("订单不存在");
                return;
            }

            Order oldOrder = orders[0];
            Console.WriteLine("当前订单信息:");
            Console.WriteLine(oldOrder);

            Console.Write("输入新客户ID: ");
            string customerId = Console.ReadLine();

            Console.Write("输入新客户姓名: ");
            string customerName = Console.ReadLine();

            Customer customer = new Customer(customerId, customerName);
            Order newOrder = new Order(orderId, customer, DateTime.Now);

            Console.WriteLine("请重新添加订单明细:");
            while (true)
            {
                Console.Write("是否添加商品明细? (y/n): ");
                if (Console.ReadLine().ToLower() != "y") break;

                Console.Write("输入商品ID: ");
                string productId = Console.ReadLine();

                Console.Write("输入商品名称: ");
                string productName = Console.ReadLine();

                Console.Write("输入商品价格: ");
                decimal price = decimal.Parse(Console.ReadLine());

                Console.Write("输入数量: ");
                int quantity = int.Parse(Console.ReadLine());

                Product product = new Product(productId, productName, price);
                OrderDetails detail = new OrderDetails(product, quantity);

                try
                {
                    newOrder.AddDetail(detail);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine($"错误: {ex.Message}");
                }
            }

            try
            {
                orderService.UpdateOrder(newOrder);
                Console.WriteLine("订单修改成功!");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"错误: {ex.Message}");
            }
        }

        static void QueryOrders()
        {
            Console.WriteLine("\n查询订单");
            Console.WriteLine("1. 按订单号查询");
            Console.WriteLine("2. 按商品名称查询");
            Console.WriteLine("3. 按客户查询");
            Console.WriteLine("4. 按金额范围查询");
            Console.Write("请选择查询方式: ");

            string choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    Console.Write("输入订单号: ");
                    string orderId = Console.ReadLine();
                    DisplayOrders(orderService.QueryByOrderId(orderId));
                    break;
                case "2":
                    Console.Write("输入商品名称: ");
                    string productName = Console.ReadLine();
                    DisplayOrders(orderService.QueryByProductName(productName));
                    break;
                case "3":
                    Console.Write("输入客户姓名: ");
                    string customerName = Console.ReadLine();
                    DisplayOrders(orderService.QueryByCustomer(customerName));
                    break;
                case "4":
                    Console.Write("输入最小金额: ");
                    decimal minAmount = decimal.Parse(Console.ReadLine());
                    Console.Write("输入最大金额: ");
                    decimal maxAmount = decimal.Parse(Console.ReadLine());
                    DisplayOrders(orderService.QueryByAmountRange(minAmount, maxAmount));
                    break;
                default:
                    Console.WriteLine("无效的选择");
                    break;
            }
        }

        static void DisplayAllOrders()
        {
            Console.WriteLine("\n所有订单:");
            DisplayOrders(orderService.GetAllOrders());
        }

        static void DisplayOrders(List<Order> orders)
        {
            if (orders.Count == 0)
            {
                Console.WriteLine("没有找到符合条件的订单");
                return;
            }

            foreach (var order in orders)
            {
                Console.WriteLine(order);
                Console.WriteLine("----------------------");
            }
        }

        static void SortOrders()
        {
            Console.WriteLine("\n排序订单");
            Console.WriteLine("1. 按订单号排序");
            Console.WriteLine("2. 按总金额排序");
            Console.WriteLine("3. 按订单日期排序");
            Console.Write("请选择排序方式: ");

            string choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    orderService.SortOrders();
                    Console.WriteLine("已按订单号排序");
                    break;
                case "2":
                    orderService.SortOrders((o1, o2) => o1.TotalAmount.CompareTo(o2.TotalAmount));
                    Console.WriteLine("已按总金额排序");
                    break;
                case "3":
                    orderService.SortOrders((o1, o2) => o1.OrderDate.CompareTo(o2.OrderDate));
                    Console.WriteLine("已按订单日期排序");
                    break;
                default:
                    Console.WriteLine("无效的选择");
                    break;
            }
        }
    }
}
